<template>
  <header class="header">
    <h1>todos</h1>
    <input v-model="isAll" id="toggle-all" class="toggle-all" type="checkbox" />
    <label for="toggle-all"></label>
    <input
      @keyup.enter="add"
      v-model.trim="content"
      class="new-todo"
      placeholder="输入任务名称-回车确认"
      autofocus
    />
  </header>
</template>

<script>
export default {
  computed: {
    isAll: {
      set(val) {
        console.log(val);
        this.list.forEach((item) => (item.isDone = val));
      },
      get() {
        return this.list.every((item) => item.isDone);
      },
    },
  },
  methods: {
    add() {
      //判断有无内容
      if (this.content.length === 0) return;

      //添加数据，就是在修改原数据，不能直接在子组件中修改，this.$emit()
      this.$emit("add", this.content);
      //添加之后，清空input框
      this.content = "";
    },
  },
  props: ["list"],
  data() {
    return {
      content: "",
    };
  },
};
</script>
